<?php

namespace App\Admin\Controllers\Seckill;

use App\Admin\Actions\Post\BatchRestore;
use App\Admin\Actions\Post\Restore;
use App\Admin\Repositories\CmcStoreSeckill;

use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB as CmcGoods;

class CmcStoreSeckillController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CmcStoreSeckill(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('seckill_name')->editable(true);
            $grid->column('goods_name','商品名称');
            $grid->column('seckill_info');
            $grid->column('images')->image($server=null,100,100);
            $grid->column('price');
            $grid->column('stock');
            $grid->column('status', '秒杀状态')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'已结束',
                    1=>'正在进行'
                ])
            );
            // 搜索
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->equal('goods_name','商品名称');
                $filter->equal('is_show','是否显示');
                $filter->equal('status','秒杀状态')->select(['0'=>'已结束','1'=>'正在进行']);
            });
            $grid->options()->select([
                1 => 'Sed ut perspiciatis unde omni',
                2 => 'voluptatem accusantium doloremque',
                3 => 'dicta sunt explicabo',
                4 => 'laudantium, totam rem aperiam',
            ]);
            // 模糊搜索
            $grid->quickSearch(['seckill_name', 'goods_name']);
            $grid->column('start_time');
            $grid->column('end_time');
            $grid->column('updated_at')->sortable();
            // 倒序排序
            $grid->model()->orderBy('created_at', 'desc');
            // 回收站
            $grid->filter(function ($filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\Seckill\CmcStoreSeckill::class));
                }
            });
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(\App\Models\Seckill\CmcStoreSeckill::class));
                }
            });
            // 异步渲染功能
            $grid->async();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmcStoreSeckill(), function (Show $show) {
            $show->field('id');
            $show->field('seckill_name');
            $show->field('seckill_info');
            $show->field('images');
            $show->field('price');
            $show->field('sort');
            $show->field('status');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('goods_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(CmcStoreSeckill::with('SeckillGoods'), function (Form $form) {
            $form->display('id');
            $form->text('seckill_name')->rules('required');
            $form->text('seckill_info')->rules('required');
            $form->select('category_name')->options(
                CmcGoods::table('cmc_goods_category')->where('is_show',1)->pluck('category_name','category_name')
            );
            $form->select('goods_name')->options(
                CmcGoods::table('cmc_goods')->where('goods_state',1)->pluck('goods_name','goods_name')
            );
            $form->image('images')->rules('required');
            $form->text('price')->rules('required');
            $form->text('sort')->rules('required');
            $form->text('status')->rules('required');
            $form->text('goods_id')->rules('required');
            $form->datetime('start_time')->rules('required');
            $form->datetime('end_time')->rules('required');
            $form->display('created_at');
            $form->display('updated_at');
            $form->confirm('您确定要提交表单吗？');
        });
    }
}
